// pages/sangcheng/sangcheng.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    openid: '',
    quyuxin: '', //区域
    shaopi: '', //商品
    simk: '', //四个模块
    spxing: '',
    Getphone: false,
    huobo: '', //火爆单品
    youmi: '', //邮米
    lunzhan: '', //轮播右
    allGoods: [], //所有商品
  },


  diao_s(e) {
    console.log(e.currentTarget.dataset.item);
    this.setData({
      spxing: e.currentTarget.dataset.item
    })
    wx.navigateTo({
      url: '../../pages/shangpiny/shangpiny?data=' + JSON.stringify(this.data.spxing)
    })

  },

  gengduoa() {
    wx.navigateTo({
      url: '../../pages/shangpiny/shangpiny?data=' + JSON.stringify(this.data.simk[0])
    })

  },
  gengduob() {
    wx.navigateTo({
      url: '../../pages/shangpiny/shangpiny?data=' + JSON.stringify(this.data.simk[1])
    })

  },

  gengduoc() {
    wx.navigateTo({
      url: '../../pages/shangpiny/shangpiny?data=' + JSON.stringify(this.data.simk[2])
    })

  },




  hqshang() {
    wx.request({
      url: app.globalData.interface+'/newVersion/API_2001_getGoodsList',
      method: 'GET',
      data: {
        OPENID: this.data.openid,
        SHAANXICITY: this.data.quyuxin,
        GOODS_CATEGORY_ID: '',
        OPEN_ID: this.data.openid, //用户OPEN_ID
        session_key: this.data.session_key,
        OPEN_UUID: this.data.jiami
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res);
        console.log(res.data.allGoods);
        // var shaopi = res.data.allGoods.slice(0, 3)
        // var huobo = res.data.pageDataGoodsType2.slice(0, 3)
        // var youmi = res.data.pageDataGoodsType3.slice(0, 3)
        // var lunzhan = res.data.pageDataGoodsType4.slice(0, 3)

        // console.log(shaopi);

        this.setData({
          allGoods: res.data.allGoods.slice(0, 3),
          allGooda: res.data.pageDataGoodsType2,
          allGoodb: res.data.pageDataGoodsType3,

          // huobo:huobo,//火爆单品
          // youmi:youmi,//邮米
          // lunzhan:lunzhan,//轮播右边
          // shaopi: shaopi,
          simk: res.data.data
        })
      }
    })

  },
  //火爆单品
  exhibits(e) {
    console.log(e.currentTarget.dataset.item);
    this.setData({
      details: e.currentTarget.dataset.item
    })

    wx.navigateTo({
      url: '../../pages/spxiangq/spxiangq?data=' + JSON.stringify(this.data.details)
    })

  },



  //获取手机号
  getPhoneNumber(e) {
    console.log(e);
    console.log(e.detail.code)
    this.setData({
      code: e.detail.code
    })
    wx.request({
      url: app.globalData.interface+'/post/API_1032_getWeiXinPhone',
      method: 'GET',
      //向后端发送的数据
      data: {
        code: this.data.code, //将code发送到后台服务器。
        OPEN_ID: this.data.openid, //用户OPEN_ID
        session_key: this.data.session_key,
        OPEN_UUID: this.data.jiami
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded" //POST方式是这个
      },
      success: res => {
        console.log(res);
        console.log(res.data.phone);
        if (res.data.result != 'success') {
          wx.showToast({
            title: '获取手机号失败',
            duration: 1000
          })
        } else {
          this.setData({
            phone: res.data.phone,
            Getphone: true,
          })
          this.baocunphone() //保存手机号接口
          // wx.navigateTo({
          //   url: '../../pages/qliebiao/qliebiao?data=' + JSON.stringify(this.data.phone)
          // })

        }
      }
    })
  },



  // 判断是否授权
  scanCod() {
    var that = this
    wx.request({
      url: app.globalData.interface+'/newVersion/API_2007_getPhoneByOpenId',
      method: 'POST',
      //向后端发送的数据
      data: {
        OPEN_ID: this.data.openid, //将code发送到后台服务器。

        session_key: this.data.session_key,
        OPEN_UUID: this.data.jiami
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded" //POST方式是这个
      },
      success: res => {
        console.log(res);
        console.log(res.data.data.CUSTOMER_TEL);
        if (res.data.result != 'success') {
          wx.showToast({
            title: '请获取手机号',
            duration: 1000
          })
          that.setData({

            Getphone: false,
          })

        } else {


          var Integral = res.data.data.INTEGRAL;
          app.globalData.Integral = Integral;
          that.setData({
            LEVEL_IMG: res.data.data.LEVEL_IMG,
            CUSTOMER_TEL: res.data.data.CUSTOMER_TEL,
            Getphone: true,
          })

        }
      }
    })
  },

  baocunphone() {
    wx.request({
      url: app.globalData.interface+'/newVersion/API_2011_savePhoneAndOpenId',
      method: 'POST',
      //向后端发送的数据
      data: {
        CUSTOMER_TEL: this.data.phone,
        OPEN_ID: this.data.openid,
        KEY: 'xianjgjKey9999',
        OPEN_ID: this.data.openid, //用户OPEN_ID
        session_key: this.data.session_key,
        OPEN_UUID: this.data.jiami
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded" //POST方式是这个
      },
      success: res => {
        console.log(res);
      }
    })

  },






  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      quyuxin: app.globalData.quyuxin,
      Integral: app.globalData.Integral,
      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      openid: app.globalData.openid,
      session_key: app.globalData.session_key,
    });
    console.log(this.data.quyuxin);
    console.log(this.data.Integral);
    console.log(this.data.session_key);


    var that = this


    if (that.data.jiami == '' || that.data.session_key == '') {


      wx.login({
        success: function (res) {
          console.log(res);
          if (res.code) { //wx.login获取code。
            console.log(res.code);
            //发起网络请求
            wx.request({
              url: app.globalData.interface+'/post/API_1062_appGetOpenid',
              method: 'GET',
              //向后端发送的数据
              data: {
                code: res.code //将code发送到后台服务器。
              },
              header: {
                "Content-Type": "application/x-www-form-urlencoded" //POST方式是这个
              },
              success: res => {
                console.log(res);
                if (res.data.result != 'success') {
                  wx.showToast({
                    title: '请获取手机号',
                    duration: 1000
                  })
                } else {
                  console.log(res.data.openid);
                  let openid = res.data.openid;
                  app.globalData.openid = openid;
                  that.setData({
                    openid: res.data.openid
                  })
                  that.hqshang()
                  that.scanCod()
                }
              }
            })
          } else {
            console.log('获取用户登录态失败！' + res.errMsg)
          }
        }
      });
    } else {


      that.hqshang()
      that.scanCod()
      console.log('成功');
    }



    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})